package controllers;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import models.Attendant;
import models.Course;
import models.TeachingSession;
import data.AttendantDAO;
import data.CourseDAO;
import data.IAttendantDAO;
import data.ICourseDAO;
import spark.Request;
import spark.Response;

public class AttendantController extends BaseController {
	private IAttendantDAO dao;
	private ICourseDAO courseDao;
	public AttendantController(Request request, Response response) {
		super(request, response);
		dao = new AttendantDAO();
		courseDao = new CourseDAO();
	}
	
	public Object list() {
		Map<String, Object> attributes = new HashMap<>();
		if(request.requestMethod().toUpperCase().equals("GET")){
			try{
				List<Course> courses;
				if(this.isUserInRole("Admin")){
					courses = courseDao.list();
				} else if(this.isUserInRole("Tutor") || this.isUserInRole("Lecturer")){
					courses = courseDao.listTeacherCourses(this.getCredentials().getPersonId());
				} else {
					courses = courseDao.listStudentCourses(this.getCredentials().getPersonId());
				}
				
				attributes.put("courses", courses);
				attributes.put("courseId", 0);
			}catch(Exception e) {
				attributes.put("error", e.getMessage());
				return render(attributes, "error.html");
			}
		
		} else if(request.requestMethod().toUpperCase().equals("POST")) {
			int courseId = Integer.parseInt( request.queryParams("courseId"));
			
			List<TeachingSession> sessions;
			if(this.isUserInRole("Tutor")){
				sessions = courseDao.listTutorSessions(courseId, this.getCredentials().getPersonId());
			}else {
				sessions = courseDao.listSessions(courseId);
			}
			
			List<Course> courses;
			if(this.isUserInRole("Admin")){
				courses = courseDao.list();
			} else if(this.isUserInRole("Tutor") || this.isUserInRole("Lecturer")){
				courses = courseDao.listTeacherCourses(this.getCredentials().getPersonId());
			} else {
				courses = courseDao.listStudentCourses(this.getCredentials().getPersonId());
			}
			
			attributes.put("courses", courses);
			attributes.put("sessions", sessions);
			attributes.put("courseId", courseId);
		}
		return render(attributes, "attendant/list.html");
	}
	
	public Object generateAttendantRecords(){
		int sessionId = Integer.parseInt( request.params(":sessionId"));
		int courseId = Integer.parseInt( request.params(":courseId"));
		dao.generateAttendants(courseId, sessionId, dao.getStudentIds(courseId));
		redirect("attendants/" + courseId + "/" + sessionId);
		return "";
	}
	
	public Object manageSessionAttendants() {
		Map<String, Object> attributes = new HashMap<>();
		if(request.requestMethod().toUpperCase().equals("GET")){
			try{
				int sessionId = Integer.parseInt( request.params(":sessionId"));
				int courseId = Integer.parseInt( request.params(":courseId"));
				
				List<Attendant> attendants;
				if(this.isUserInRole("Student")){
					attendants = dao.listStudentAttendants(sessionId, this.getCredentials().getPersonId());
				} else if(this.isUserInRole("Tutor")){
					attendants = dao.listTutorAttendants(sessionId, this.getCredentials().getPersonId());
				} else {
					attendants = dao.list(sessionId);
				}
				TeachingSession session = new TeachingSession();
				session.setId(sessionId);
				session.setAttendants(attendants);
				
				attributes.put("session", session);
				attributes.put("courseId", courseId);
				return render(attributes, "attendant/manage_session_attendants.html");
			}catch(Exception e) {
				attributes.put("error", e.getMessage());
				return render(attributes, "error.html");
			}
		} else {
			String[] array = request.raw().getParameterValues("ids");
			List<Attendant> attendants = new LinkedList<>();
			for(String record : array){
				String[] tuple = record.split("-");
				Attendant a = new Attendant();
				a.setId(Integer.parseInt(tuple[0]));
				a.setStatus(Integer.parseInt(tuple[1]));
				attendants.add(a);
			}
			dao.updateAttendants(attendants);
			redirect(this.getCurrentRequestUrl());
			return "";
		}
		
	}

}
